﻿var infoWindow;
var point_arry = undefined,
    lng = undefined,
    lat = undefined,
    data = undefined;

//请求该账户下的所有站场信息
App.ajax({
    data: {},
    resUrl: "/station/allList",
    callback: function(rb){
        if(!rb.status) return;
        data = rb.data;
        point_arry = data[0].site.split(",");
        lng = point_arry[0];
        lat = point_arry[1];
        createMap();
    }
});
function createMap() {
    var map = new AMap.Map("container_goMap", {
        resizeEnable: true,
        center: [lng, lat],//[116.473188, 39.993253],
        zoom: 13
    });
    addMarker();
    function addMarker() {
        map.clearMap();
        /*var marker = new AMap.Marker({
         map: map,
         position: [lng, lat]//[116.473188, 39.993253]
         });*/
        for (var i = 0, marker; i < data.length; i++) {
            var point_arry1 = data[i].site.split(",");
            var lng1 = point_arry1[0];
            var lat1 = point_arry1[1];
            var marker = new AMap.Marker({
                map: map,
                position: [lng1, lat1]
            });

            marker.content = i;
            marker.on("click", markerClick);
            marker.emit("click", {target: marker});
        }
        openInfo(data[0]);
        infoWindow.open(map, map.getCenter());
    }
    function markerClick(e){
        var i = e.target.content;
        openInfo(data[i]);
        infoWindow.open(map, e.target.getPosition());
    }
    //构建自定义信息窗体
    function createInfoWindow(title, content) {
        var info = document.createElement("div");
        info.className = "info";

        //可以通过下面的方式修改自定义窗体的宽高
        //info.style.width = "400px";
        // 定义顶部标题
        var top = document.createElement("div");
        var titleD = document.createElement("div");

        top.className = "info-top";
        titleD.innerHTML = title;

        top.appendChild(titleD);
        info.appendChild(top);

        // 定义中部内容
        var middle = document.createElement("div");
        middle.className = "info-middle";
        middle.style.backgroundColor = 'white';
        middle.innerHTML = content;
        info.appendChild(middle);

        // 定义底部内容
        var bottom = document.createElement("div");
        bottom.className = "info-bottom";
        bottom.style.position = 'relative';
        bottom.style.top = '0px';
        bottom.style.margin = '0 auto';
        var sharp = document.createElement("img");
        sharp.src = "http://webapi.amap.com/images/sharp.png";
        bottom.appendChild(sharp);
        info.appendChild(bottom);
        return info;
    }

    //在指定位置打开信息窗体
    function openInfo(d) {
         var address = d.province + d.city + d.area + d.address;
         //构建信息窗体中显示的内容
         var title = d.name + '<span style="font-size:11px;color:#F00;"></span>',
         content = [];
         content.push("<img src='"+ d.picture +"' style='width:60px;height:60px;'>地址：" + address);
         content.push("<a href='javascript:edit("+ d.id+");'>编辑</a>");
         content.push("");
         infoWindow = new AMap.InfoWindow({
             isCustom: true,  //使用自定义窗体
             content: createInfoWindow(title, content.join("<br/>")),
             offset: new AMap.Pixel(16, -45)
         });
         //infoWindow.open(map, map.getCenter());
    }
     //openInfo();
}
function edit(id){
    var obj ={
        "id":id
    }
    window.parent.App.mapToeditPanel.notify({ "res": obj })
}